﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pattern_Iterator2
{
    abstract class Aggregate
    {
        public abstract Iterator CreateIterator();
    }

    class Aggregate1 : Aggregate
    {
        IList<object> items = new List<object>();

        public override Iterator CreateIterator()
        {
            return new Iterator1(this);
        }

        public int Count
        {
            get { return items.Count; }
        }

        public object this[int index]
        {
            get { return items[index]; }
            set { items.Insert(index, value); }
        }
    }

    abstract class Iterator
    {
        public abstract object First();
        public abstract object Next();
        public abstract bool IsDone();
        public abstract object CurrentItem();
    }

    class Iterator1 : Iterator
    {
        int current = 0;
        Aggregate1 aggreagate;

        public Iterator1(Aggregate1 aggreagate1)
        {
            this.aggreagate = aggreagate1;
        }


        public override object First()
        {
            return aggreagate[0];
        }

        public override object Next()
        {
            if (++current < aggreagate.Count)
            {
                return aggreagate[current];
            }
            return null;
        }

        public override bool IsDone()
        {
            if (current >= aggreagate.Count)
                return true;
            else
                return false;
        }

        public override object CurrentItem()
        {
            return aggreagate[current];
        }
    
    }
}
